{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    \"detect\": [\n",
    "        {\n",
    "            \"class_index\": 10,\n",
    "            \"confidence\": 0.9459573030471802,\n",
    "            \"box\": [\n",
    "                6,\n",
    "                2012,\n",
    "                1087,\n",
    "                3138\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"class_index\": 10,\n",
    "            \"confidence\": 0.8741680383682251,\n",
    "            \"box\": [\n",
    "                312,\n",
    "                1070,\n",
    "                885,\n",
    "                2213\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"class_index\": 10,\n",
    "            \"confidence\": 0.8437256217002869,\n",
    "            \"box\": [\n",
    "                607,\n",
    "                2964,\n",
    "                2253,\n",
    "                4023\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"class_index\": 10,\n",
    "            \"confidence\": 0.5008958578109741,\n",
    "            \"box\": [\n",
    "                860,\n",
    "                923,\n",
    "                1987,\n",
    "                2739\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"class_index\": 14,\n",
    "            \"confidence\": 0.5314203500747681,\n",
    "            \"box\": [\n",
    "                1642,\n",
    "                692,\n",
    "                1675,\n",
    "                761\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"class_index\": 15,\n",
    "            \"confidence\": 0.49921488761901855,\n",
    "            \"box\": [\n",
    "                1205,\n",
    "                1304,\n",
    "                1238,\n",
    "                1351\n",
    "            ]\n",
    "        }\n",
    "    ],\n",
    "    \"count\": {\n",
    "        10: 4, # json保存后为 \"10\": 4\n",
    "        14: 1,\n",
    "        15: 1\n",
    "    },\n",
    "    \"image_size\": [\n",
    "        4032,\n",
    "        2268,\n",
    "        3\n",
    "    ]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reformat(data: dict) -> dict:\n",
    "    new_data = {}\n",
    "    detect = []\n",
    "    for k in data[\"count\"].keys():                      # 循环count的key\n",
    "        class_ids  = []\n",
    "        confidence = []\n",
    "        class_dets = []\n",
    "        for det in data[\"detect\"]:                      # 循环所有框\n",
    "            if k == det[\"class_index\"]:                 # 通过count的k和框的id匹配,确定同一个类别\n",
    "                class_ids.append(det[\"class_index\"])\n",
    "                confidence.append(det[\"confidence\"])\n",
    "                class_dets.append(det[\"box\"])\n",
    "\n",
    "        class_ids = list(set(class_ids))                # 去重id\n",
    "        confidence = [max(confidence)]                  # 找最大得分\n",
    "\n",
    "        detect.append({\"class_index\": class_ids, \"confidence\": confidence, \"box\": class_dets})\n",
    "\n",
    "    new_data[\"detect\"]     = detect\n",
    "    new_data[\"count\"]      = data[\"count\"]\n",
    "    new_data[\"image_size\"] = data[\"image_size\"]\n",
    "\n",
    "    return new_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'detect': [{'class_index': [10],\n",
       "   'confidence': [0.9459573030471802],\n",
       "   'box': [[6, 2012, 1087, 3138],\n",
       "    [312, 1070, 885, 2213],\n",
       "    [607, 2964, 2253, 4023],\n",
       "    [860, 923, 1987, 2739]]},\n",
       "  {'class_index': [14],\n",
       "   'confidence': [0.5314203500747681],\n",
       "   'box': [[1642, 692, 1675, 761]]},\n",
       "  {'class_index': [15],\n",
       "   'confidence': [0.49921488761901855],\n",
       "   'box': [[1205, 1304, 1238, 1351]]}],\n",
       " 'count': {10: 4, 14: 1, 15: 1},\n",
       " 'image_size': [4032, 2268, 3]}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_data = reformat(data)\n",
    "new_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"detect\": [\n",
      "        {\n",
      "            \"class_index\": [\n",
      "                10\n",
      "            ],\n",
      "            \"confidence\": [\n",
      "                0.9459573030471802\n",
      "            ],\n",
      "            \"box\": [\n",
      "                [\n",
      "                    6,\n",
      "                    2012,\n",
      "                    1087,\n",
      "                    3138\n",
      "                ],\n",
      "                [\n",
      "                    312,\n",
      "                    1070,\n",
      "                    885,\n",
      "                    2213\n",
      "                ],\n",
      "                [\n",
      "                    607,\n",
      "                    2964,\n",
      "                    2253,\n",
      "                    4023\n",
      "                ],\n",
      "                [\n",
      "                    860,\n",
      "                    923,\n",
      "                    1987,\n",
      "                    2739\n",
      "                ]\n",
      "            ]\n",
      "        },\n",
      "        {\n",
      "            \"class_index\": [\n",
      "                14\n",
      "            ],\n",
      "            \"confidence\": [\n",
      "                0.5314203500747681\n",
      "            ],\n",
      "            \"box\": [\n",
      "                [\n",
      "                    1642,\n",
      "                    692,\n",
      "                    1675,\n",
      "                    761\n",
      "                ]\n",
      "            ]\n",
      "        },\n",
      "        {\n",
      "            \"class_index\": [\n",
      "                15\n",
      "            ],\n",
      "            \"confidence\": [\n",
      "                0.49921488761901855\n",
      "            ],\n",
      "            \"box\": [\n",
      "                [\n",
      "                    1205,\n",
      "                    1304,\n",
      "                    1238,\n",
      "                    1351\n",
      "                ]\n",
      "            ]\n",
      "        }\n",
      "    ],\n",
      "    \"count\": {\n",
      "        \"10\": 4,\n",
      "        \"14\": 1,\n",
      "        \"15\": 1\n",
      "    },\n",
      "    \"image_size\": [\n",
      "        4032,\n",
      "        2268,\n",
      "        3\n",
      "    ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(new_data, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
